'use strict';
app.controller('bankCardCtrl',['$scope','utilService','bankCardService','toaster','$modal',
    function($scope,utilService,bankCardService,toaster,$modal){

    //查询银行卡信息
    $scope.bank={};
    $scope.findAll=function(){
        bankCardService.findAll($scope).then(function(result){
            switch (result.status){
                case 200:
                    var info=result.data;
                    for(var i=0;i<info.length;i++){
                        info[i].bankCard=info[i].bankCard.substring(0,4)+' **** **** *** '+info[i].bankCard.substring(15,19);
                    }
                    $scope.bank=info;
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.findAll();

    //删除银行卡
    $scope.bankCardDel = function(id){
        // query data.
        openModal("确定要解绑该银行卡吗？");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"message",
                controller:"deleteCtrl",
                message:msg,
                data:id
            });
        }
    }
}]);

app.controller('deleteCtrl', ['$scope', '$modalInstance','$state','bankCardService','toaster','modalData',
    function($scope, $modalInstance,$state,bankCardService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.cardId=modalData.data;
        $scope.isDelModal=true;

        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            bankCardService.bankCardDel($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.isDelModal=true;
                        $scope.findAll();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);

app.controller('bankCardAddCtrl',['$scope','utilService','bankCardService','toaster','$modal','$interval','$state',
    function($scope,utilService,bankCardService,toaster,$modal,$interval,$state){

    $scope.type={
      state:0
    };

    $scope.bankCardVO={
        status:$scope.type.state,
        count:$scope.type.count
    };
    $scope.find=function(){
        bankCardService.find($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.type.count=result.data.count;
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };
    $scope.find();

    $scope.description = "获取验证码";
    $scope.check = function(){
        if($scope.bankCardVO.companyPhone!= null){
            $scope.bankCardVO.mobilePhone=$scope.bankCardVO.companyPhone;
        }else if($scope.bankCardVO.userPhone != null){
            $scope.bankCardVO.mobilePhone=$scope.bankCardVO.userPhone;
        }
        $scope.canClick=false;
        var second=59;
        var timerHandler;
        bankCardService.getMobilePhoneCode($scope).then(function(result){
            switch (result.status){
                case 200:
                    timerHandler = $interval(function(){
                        if(second<=0){
                            $interval.cancel(timerHandler);//当执行的时间59s,结束时，取消定时器 ，cancle方法取消
                            second=59;
                            $scope.description="获取验证码";
                            $scope.canClick=false;    //因为 ng-disabled属于布尔值，设置按钮可以点击，可点击发送
                        }else{
                            $scope.description=second+"s后重发";
                            second--;
                            $scope.canClick=true;
                        }
                    },1000)   //每一秒执行一次$interval定时器方法
                    break;
                case 680:
                    toaster.pop('error', "操作失败", "不能频繁发送验证码,请稍后再发");
                    break;
                case 697:
                    toaster.pop('error', "操作失败", "该店铺已绑定该银行卡,请更换银行卡绑定");
                    break;
                default :
                    alert("发生未知错误！请联系管理员");
                    break;
            }
        });
    };

    //验证银行卡四要素
    $scope.confirmInfo=function(){
        bankCardService.confirmInfo($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.openAdd(result.data);
                    $scope.find();
                    break;
                case 693:
                    toaster.pop('error', "操作失败", "没有找到相匹配的银行，请检查填写的信息是否正确统一！");
                    $scope.find();
                    break;
                case 696:
                    toaster.pop('error', "操作失败", "验证银行卡失败！");
                    $scope.find();
                    break;
                case 647:
                    toaster.pop('error', "操作失败", "验证码不一致");
                    $scope.find();
                    break;
                case 408:
                    toaster.pop('error', "操作失败", "验证码已过期");
                    $scope.find();
                    break;
                case 697:
                    toaster.pop('error', "操作失败", "该店铺已绑定该银行卡,请更换银行卡绑定");
                    $scope.find();
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.find();
                    break;
            }
        })
    };

    //验证企业银行卡
    $scope.confirmBank=function(){
        if($scope.bankCardVO.companyPhone != null){
            $scope.bankCardVO.mobilePhone=$scope.bankCardVO.companyPhone;
        }else if($scope.bankCardVO.userPhone != null){
            $scope.bankCardVO.mobilePhone=$scope.bankCardVO.userPhone;
        }
        bankCardService.confirmBank($scope).then(function(result){
            switch (result.status){
                case 200:
                    if(result.data == null){
                        toaster.pop('success', "操作成功", "绑定企业银行卡成功！");
                        $state.go('app.bankCard');
                    }else if(result.data != null && result.data.code == 1){
                        $scope.bankCardVO.requestNo = result.data.requestNo;
                        $scope.openAgain($scope.bankCardVO);
                    }
                    $scope.find();
                    break;
                case 693:
                    toaster.pop('error', "操作失败", "没有找到相匹配的银行，请检查填写的信息是否正确统一！");
                    $scope.find();
                    break;
                case 697:
                    toaster.pop('error', "操作失败", "该店铺已绑定该银行卡,请更换银行卡绑定");
                    $scope.find();
                    break;
                case 696:
                    toaster.pop('error', "操作失败", "验证失败！请填写的信息是否准确");
                    $scope.find();
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.find();
                    break;
            }
        })
    };

    //打开确认框
    $scope.openAdd = function (data) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/property/bank_card_info.html',
            controller: 'bankCardInfoCtrl',
            size: 'md',
            scope:$scope,
            resolve: {
                data: function () {
                    return data;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };

    $scope.openAgain = function(bankCardVO){
        openModal("正在验证中...(请稍后)");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"温馨提示",
                controller:"checkCtrl",
                message:msg,
                data: bankCardVO
            });
        }
    };

    $scope.change=function(){
        delete $scope.bankCardVO;
        $scope.bankCardVO={
            status:$scope.type.state,
            count:$scope.type.count
        };
    }
}]);

app.controller('checkCtrl',['$scope','$state','bankCardService','toaster','modalData','$modalInstance','$timeout',
    function($scope,$state,bankCardService,toaster,modalData,$modalInstance,$timeout){
    $scope.modal = modalData;
    $scope.bankCardVO = modalData.data;
    $scope.isDelModal=false;

    $scope.checkAgain=function(){
        var timerHandler;
        bankCardService.checkAgain($scope).then(function(result){
            switch (result.status){
                case 200:
                    if(result.data!=null && result.data.code == 1){
                        timerHandler= $timeout(function(){
                            $scope.checkAgain(result.data.requestNo);
                        },1000);   //一秒后执行$timeout定时器方法
                    }else if(result.data == null){
                        $scope.ok();
                        toaster.pop('success', "操作成功", "绑定企业银行卡成功！");
                        $state.go('app.bankCard');
                    }
                    break;
                case 693:
                    $scope.cancel();
                    toaster.pop('error', "操作失败", "没有找到相匹配的银行，请检查填写的信息是否正确统一！");
                    break;
                case 696:
                    $scope.cancel();
                    toaster.pop('error', "操作失败", "验证失败！请确保填写的信息准确");
                    break;
                case 702:
                    $scope.cancel();
                    toaster.pop('error', "操作失败", "收款银行不存在此银行账号，请核对收款银行与银行账号是否填写正确");
                    break;
                default:
                    $scope.cancel();
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.checkAgain();

    //确认弹窗
    $scope.ok = function () {
        $modalInstance.dismiss('success');
    };

    //取消弹窗
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

app.controller('bankCardInfoCtrl',['$scope','$state','bankCardService','toaster','data','$modalInstance',
    function($scope,$state,bankCardService,toaster,data,$modalInstance){
    $scope.bankInfo=data;

    $scope.ok = function(){
        bankCardService.confirm($scope).then(function(result){
            switch (result.status){
                case 200:
                    $modalInstance.dismiss('success');
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.bankCard');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };

    //取消弹窗
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

}]);